
<!-- （Modal） -->
<div class="modal fade" id="sharingModal" tabindex="-1" role="dialog"
     aria-labelledby="sharingModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"
                aria-hidden="true">
          ×
        </button>
        <h4 class="modal-title" id="sharingModalLabel">
          <span class="glyphicon glyphicon-share"></span> 共享
        </h4>
      </div>
      <div class="modal-body">
        <div class="row container-fluid">
          <form class="form-horizontal" action="/xms/api/data/share" data-jsonajax="true" method="post">
            <input type="hidden" name="entityid" />
            <input type="hidden" name="objectid" />
            <input type="hidden" name="principalsjson" />
            <div class="form-group col-sm-12">
              <div class="btn-group">
                <button type="button" class="btn btn-primary btn-xs" name="addBtn">
                  <span class="glyphicon glyphicon-plus-sign"></span> 添加用户
                </button>
                <button type="button" class="btn btn-warning btn-xs" name="removeBtn">
                  <span class="glyphicon glyphicon-trash"></span> 移除
                </button>
                <button type="button" class="btn btn-default btn-xs" name="clearBtn">
                  <span class="glyphicon glyphicon-refresh"></span> 清空
                </button>
              </div>
            </div>
            <div class="form-group col-sm-12">
              <div class="table-responsive">
                <table class="table table-striped table-bordered" id="datatable">
                  <thead>
                  <tr>
                    <th><input type="checkbox" name="checkall" /></th>
                    <th>名称</th>
                    <th>读取</th>
                    <th>修改</th>
                    <th>删除</th>
                    <th>分派</th>
                    <th>共享</th>
                    <th>追加</th>
                  </tr>
                  </thead>
                  <tbody>
                  <tr class="hide">
                    <td>
                      <input type="checkbox" name="recordid" />
                      <input type="hidden" name="principalid" />
                    </td>
                    <td>
                      管理员
                    </td>
                    <td><input type="checkbox" name="read" data-mask="1" /></td>
                    <td><input type="checkbox" name="update" data-mask="3" /></td>
                    <td><input type="checkbox" name="delete" data-mask="4" /></td>
                    <td><input type="checkbox" name="assign" data-mask="5" /></td>
                    <td><input type="checkbox" name="share" data-mask="6" /></td>
                    <td><input type="checkbox" name="append" data-mask="7" /></td>
                  </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </form>
        </div>
        <script src="/content/js/jquery.form.js"></script>
        <script>
          var SharingModel = {
            dialog: $('#sharingModal'),
            dialogid: 'sharingModal',
            datatable: $('#sharingModal #datatable'),
            entityid: '',
            objectid: '',
            principals: [],
            init: function(){
              var paramsModel = JSON.parse(decodeURIComponent($('#sharingModal').parent().attr('data-send')));
              SharingModel.entityid = paramsModel.entityid;
              SharingModel.objectid = paramsModel.objectid;
              var url = '/api/security/principalobjectaccess/getbyobject?entityid=' + SharingModel.entityid + '&objectid=' + SharingModel.objectid;

              Xms.Web.GetJson(url, null, function(res){
                if(!res.IsSuccess){
                  Xms.Web.FailureHandler(res);
                  return;
                }
                SharingModel.principals = res.Content;
                var principalItems = [];
                $(SharingModel.principals).each(function(i, n){
                  n.id = n.principalid;
                  n.name = n.principalidname;
                  principalItems.push(n)
                  //SharingModel.bindselected({id: n.principalid, name: n.principalidname}, n);
                });
                SharingModel.bindselected(principalItems);
              });
            },
            submit: function () {
              var pjson = [];
              SharingModel.datatable.find('tbody>tr:not(.hide)').each(function (i, n) {
                var principalid = $(n).attr('data-principalid');
                $(n).find('input[data-mask]:checked').each(function(j, item){
                  var obj = {};
                  obj.principalid = principalid;
                  obj.entityid = SharingModel.entityid;
                  obj.objectid = SharingModel.objectid;
                  obj.accessrightsmask = $(item).attr('data-mask');
                  pjson.push(obj);
                });
              });
              console.log(pjson);
              SharingModel.disabledForm();
              var data = {entityid: SharingModel.entityid, objectid: SharingModel.objectid, principals: pjson};
              Xms.Web.Post('/api/data/share', data, false, function(res){
                SharingModel.resetForm();
                if(!res.IsSuccess){
                  Xms.Web.FailureHandler(res);
                  return;
                }
                else{
                  SharingModel.dialog_return();
                }
              });
            },
            disabledForm: function () {
              SharingModel.dialog.find('button:not(.close)').prop('disabled', true);
            },
            resetForm: function () {
              SharingModel.dialog.find('button').prop('disabled', false);
            },
            dialog_return: function () {
              SharingModel.dialog.modal('hide');
            },
            bindselected: function (result) {
              console.log(result);
              $(result).each(function (i, n) {
                var row = SharingModel.datatable.find('tbody>tr[data-principalid='+n.id+']');
                if(row.length > 0) {
                  row.find('input[data-mask='+n.accessrightsmask+']').prop('checked', true);
                }
                else {
                  row = SharingModel.datatable.find('tbody>tr:first').clone();
                  row.attr('data-principalid', n.id);
                  row.find('input[data-mask=' + n.accessrightsmask + ']').prop('checked', true);
                  row.find('td:eq(1)').text(n.name);
                  row.removeClass('hide');
                  row.find('input[name=principalid]').val(n.id);
                  SharingModel.datatable.find('tbody').append(row);
                }
              });
            },
            removeselected: function () {
              SharingModel.datatable.find('tbody>tr:gt(0)').each(function (i, n) {
                if ($(n).find('input[name=recordid]').is(':checked')) {
                  $(n).remove();
                }
              });
            },
            clear: function () {
              Xms.Web.Confirm(LOC_CLEAR, LOC_CONFIRM_OPERATION_TITLE, function () {
                SharingModel.datatable.find('tbody>tr:gt(0)').remove();
                SharingModel.dialog.find('form').find('input[name^="targetid"]');
              });
            }
          };
          $(function () {
            SharingModel.dialog.modal({
              backdrop: 'static'
            });
            SharingModel.dialog.on('hidden.bs.modal', function () {
              Xms.Web.CloseDialog(SharingModel.dialogid);
            });
            SharingModel.dialog.on('click', 'button[name=addBtn]', function (e) {
              Xms.Web.OpenDialog('/entity/RecordsDialog?entityname=systemuser', 'SharingModel.bindselected')
            });
            SharingModel.dialog.on('click', 'button[name=removeBtn]', function (e) {
              SharingModel.removeselected();
            });
            SharingModel.dialog.on('click', 'button[name=clearBtn]', function (e) {
              SharingModel.clear();
            });
            SharingModel.init();
          });
        </script>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default"
                data-dismiss="modal">
          <span class="glyphicon glyphicon-remove"></span>
          关闭
        </button>
        <button type="button" class="btn btn-primary" onclick="SharingModel.submit()">
          <span class="glyphicon glyphicon-ok"></span>
          确定
        </button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->